﻿using INTEX.DataBase.Tools;
using ReflectionStudio.Components.UserControls;
using ReflectionStudio.Core.Events;
using ReflectionStudio.Core.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class MVTVK02 : DocumentBase
    {
        #region-------------Properties-----------------------
        ApplicationBase app
        {
            get
            {
                return ApplicationBase.Instance;
            } 
        }
        public OracleConnection OracleDB;
        public OracleCommand orlReadCommand = new OracleCommand();
        public IntexReptServBLClient proxy;
        private Dictionary<string, object> dicParamater;
        public DataSet dsResult;
        MVTVK02Data m_Data, m_DataX;
        public DataSet setUI = new DataSet();
        public DataSet dsParamaters;
        public DataRow dwABPOS;
        public INTEXDBC INTEXDBC;
        private DataRow dwUI;
        private string fromdate = "", enddate = "";
        DataTable tableUI;
        #endregion
        public MVTVK02()
        {
            InitializeComponent();
            this.Name = "订单统计";

        }
        public override void SetUp(Dictionary<string, object> dicParamater)
        {
            base.SetUp(dicParamater);
            InitializeComponent();
            this.dicParamater = dicParamater;
            OracleDB = (OracleConnection)dicParamater["OracleDB"];
            proxy = (IntexReptServBLClient)dicParamater["proxy"];
            orlReadCommand = OracleDB.CreateCommand();
            orlReadCommand.Parameters.Add("OrdNum", OracleType.NVarChar).Value = "";
            m_Data = new MVTVK02Data();
            m_Data.OracleDB = this.OracleDB;
            m_Data.proxy = this.proxy;
            INTEXDBC = new INTEXDBC(OracleDB, proxy);
            tableUI = new DataTable("UIData");
            tableUI.Columns.Add("FROMDATE", typeof(string));
            tableUI.Columns.Add("ENDDATE", typeof(string));
        }

        private void InitDataBinding()
        {

        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                dwUI = tableUI.NewRow();
                InitDataBinding();
                if (app.User.Trim() == "JLTV")
                {
                    fromdate = "20130101";
                    enddate = "20160101";
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            } 
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            try
            {
                using (new LongOperation(this, "读取符合条件的记录"))
                {
                    DataSet setResult = new DataSet();
                    setUI.Clear();
                    setUI.AcceptChanges();
                    if (fromdate == "")
                    {
                        fromdate = "20000000";
                    }
                    if (enddate == "")
                    {
                        enddate = "99999999";
                    }
                    dwUI["FROMDATE"] = fromdate;
                    dwUI["ENDDATE"] = enddate;
                    setResult = m_Data.BuildMVTVK02Data(dwUI);
                    setUI.Merge(setResult);
                    MasterDg.DataContext = setUI.Tables["WASTAMM"];
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnEXCEL_Click(object sender, RoutedEventArgs e)
        {
            m_DataX = new MVTVK02Data(dicParamater);
            m_DataX.exAgent._app.Visible = false;
            m_DataX.OracleDB = this.OracleDB;
            m_DataX.proxy = this.proxy;
            MVTVK02Report m_rpt = new MVTVK02Report(dicParamater);
            m_rpt.dsSource = setUI;
            m_rpt.PrintType = "OpenExcel";
            m_rpt.exAgent = m_DataX.exAgent;
            m_rpt.WriteExcelReport("EXCEL");
        }

        private void FROM_DATE_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                fromdate = "";
                fromdate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void END_DATE_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            try
            {
                enddate = "";
                enddate = m_Data.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        private void btnCharting_Click(object sender, RoutedEventArgs e)
        {
            SetUpChart();
        }
        private void SetUpChart()
        {
            try
            {
                DataTable tb = setUI.Tables["WASTAMM"];
                DataTable data;
                string name = "2013";
                string X = "KDKUNDNA";
                string Y = "TOTVALUES";
            
                string filter = "ENTRYDATE >20130100 and ENTRYDATE<20140100 ";
                DataRow[] rows = tb.Select(filter); 
                if (rows.Length > 0)
                {
                    name = "2013";
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    data = rows.CopyToDataTable();
                    Chart04.SetUp(1, name, data, X, Y);
                }
                filter = "ENTRYDATE >20140100 and ENTRYDATE<20150100 ";
                rows = tb.Select(filter);
                if (rows.Length > 0)
                {
                    data = rows.CopyToDataTable();
                    name = "2014";
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    Chart04.SetUp(2, name, data, X, Y);
                }
                filter = "ENTRYDATE >20150100 and ENTRYDATE<20160100 ";
                rows = tb.Select(filter);
                if (rows.Length > 0)
                {
                    data = rows.CopyToDataTable();
                    name = "2015";
                    Chart04.RATIO = 1;
                    Chart04.Converter = 0.001;
                    Chart04.SetUp(3, name, data, X, Y); 
                }
                Chart04.Confirm();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

    }
}
